package com.campus.controller;

import com.campus.dao.SysUserMapper;
import com.campus.entry.SysUser;
import com.campus.exception.BusinessException;
import com.campus.pojo.dto.AccountDto;
import com.campus.pojo.dto.ChangePasswordDto;
import com.campus.pojo.dto.RegisterDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.AccountService;
import com.campus.util.GetUserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 登录
     * @param accountDto
     * @return {@link ResponseResult }<{@link Map }<{@link String },{@link String }>>
     */
    @PostMapping("/login")
    public ResponseResult<Map<String,String>> login(@RequestBody AccountDto accountDto){
        if (Objects.isNull(accountDto.getLoginName())){
            throw new BusinessException(ResultCode.ERROR);
        }
        Map<String, String> login = accountService.login(accountDto);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,login);
    }

    /**
     * 注册
     * @param registerDto
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PostMapping("/register")
    public ResponseResult<Object> register(@RequestBody RegisterDto registerDto){
        if (Objects.isNull(registerDto)){
            throw new BusinessException(ResultCode.ERROR);
        }
        accountService.register(registerDto);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 登出
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PostMapping("/logout")
    public ResponseResult<Object> logout(){
        accountService.logout();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 修改密码
     * @param changePasswordDto
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PostMapping("/changePassword")
    public ResponseResult<Object> changePassword(@RequestBody ChangePasswordDto changePasswordDto){
        if (Objects.isNull(changePasswordDto)){
            throw new BusinessException(ResultCode.ERROR);
        }
        accountService.changePassword(changePasswordDto);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 是否为非法用户
     * @param role
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PostMapping("/isRightRole")
    public ResponseResult<Object> isRightRole(@RequestParam String role){
        if (Objects.isNull(role)){
            throw new BusinessException(ResultCode.ERROR_ROLE);
        }
        accountService.isRightRole(GetUserIdUtil.getUserId(),role);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }
}
